package com.mew.slots.core.game.hlsd.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.hlsd.bo.HlsdBonusType;
import com.mew.slots.core.game.hlsd.bo.HlsdInRoom;
import com.mew.slots.core.game.hlsd.message.HlsdBonusPlayResponseMessage;
import com.mew.slots.core.game.hrljj.message.HrljjBonusPlayResponseMessage;
import com.mew.slots.core.game.hrljj.service.HrljjBonusService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class HlsdBonusService {
    private final int[] jpWeights = new int[]{80,60,40,20};
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message = new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsType() != HlsdBonusType.parse(player.getBonusType()).getBonusType() || winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }

        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        HlsdInRoom in = JSON.parseObject(winFree.getJson(),HlsdInRoom.class);
        if(player.getBonusType() == HlsdBonusType.Bonus.code()){
            if(player.getOtherMsg().length != 1 || in.getOpens().contains(player.getOtherMsg()[0])){
                player.setStat(ResponseCode.BounsParamInvalid.code());
                return message;
            }
            message = playBonus(player,in);
            player.getWinFree().setJson(JSON.toJSONString(in));
        }else if(player.getBonusType()== HlsdBonusType.JP.code()){

            playJPBonus(player,message);
        }else{
            player.setStat(ResponseCode.WinFreeValidError.code());
            return message;
        }
        return message;
    }
    /**
     * 普通小游戏
     * @param player
     * @param in
     * @return
     */
    private HlsdBonusPlayResponseMessage playBonus(StrategyGameVO player, HlsdInRoom in){
        HlsdBonusPlayResponseMessage message = new HlsdBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        int open = player.getOtherMsg()[0];
        message.setIndex(open);
        message.setRate(in.getBonusIcons()[open]);
        message.setAllRate(in.getAllRate()+message.getRate());
        winFree.setBounsNumber(winFree.getBounsNumber()-1);
        if(winFree.getBounsNumber()==0){
            message.setIsGameOver(1);
            message.setPayment(message.getAllRate() * winFree.getBet() * player.getLineNumber());
            player.setPaymentNumber(message.getAllRate() * winFree.getBet() * player.getLineNumber());
        }
        in.getOpens().add(open);
        in.setAllRate(message.getAllRate());
        return message;
    }
    /**
     * 奖池小游戏
     * @param player
     * @return
     */
    private void playJPBonus(StrategyGameVO player,SlotsBonusPlayResponseMessage message){
        int jpNo = GameUtil.weights(jpWeights)+1;
        message.setJpNo(jpNo);
        player.getWinFree().setBounsType(0);
        player.getWinFree().setBounsNumber(0);
        player.setWinType(SlotsWinType.Jackpot.code());
    }

    public static void main(String[] args) {
        int rate = 0;
        for (int i = 0; i < 10000000; i++) {
            List<Integer> biList = new ArrayList<>();
            biList.add(4);
            biList.add(8);
            biList.add(10);
            biList.add(15);
            biList.add(20);
            biList.add(25);
            Collections.shuffle(biList);
            rate = rate + biList.get(0) +biList.get(1) + biList.get(2) ;
        }
        System.out.println(rate/10000000);
    }
}
